package socket;

import java.io.*;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

/**
 * @author LuXingda
 * @version 1.0
 * @data 2024/8/20 10:24
 */
//客户端
public class Client {
    /*
      java.net.Socket:套接字
      它封装了TCP协议的通信细节，使用它可以和服务器建立连接并进行交互
     */
    private Socket socket;
    public Client(){
        try {
            System.out.println("正在启动中.....");
            socket = new Socket("localhost",8088);
            System.out.println("服务器连接成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void start(){
        try {
            /*
             Socket提供了一个方法:
             OutputStream getOutputStream()
             通过socket获取输出流，将写出的信息发送给服务端
             */
            OutputStream out = socket.getOutputStream();
            OutputStreamWriter osw = new OutputStreamWriter(out, StandardCharsets.UTF_8);
            BufferedWriter bw = new BufferedWriter(osw);
            PrintWriter pw = new PrintWriter(bw,true);//自动行刷新

            Scanner scan = new Scanner(System.in);

            //要求昵称不能为空,如果错误则重新输入
            System.out.println("请输入昵称:");
            String nickname = scan.nextLine();
            while (true){

                if (nickname.trim().isEmpty()) {
                    System.out.println("用户名不能为空");
                }else {
                    pw.println(nickname);
                    System.out.println("欢迎你:"+nickname+"请开始聊天吧");
                    break;
                }
                pw.println(nickname);
            }


            //启动用于读取服务端消息的线程
            ServerHandler handler = new ServerHandler();
            Thread t  = new Thread(handler);
            //设置为守护线程,这样当主线程结束时,该线程可以一同结束了(因为当前程序没有其他用户)
            t.setDaemon(true);
            t.start();


            while (true){
                String line = scan.nextLine();
                if ("exit".equalsIgnoreCase(line)) {
                    break;
                }
                pw.println(line);//发送消息给服务器
            }

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                socket.close();//进行四次挥手
            } catch (IOException e) {
                //e.printStackTrace();
            }
        }

    }

    public static void main(String[] args) {
        Client client = new Client();
        client.start();
    }

    /**
     * 该线程任务负责读取来自服务端发送过来的消息并输出到控制台上
     */
    private class ServerHandler implements Runnable{
        public void run() {
            try {
                InputStream in = socket.getInputStream();
                InputStreamReader isr = new InputStreamReader(in,StandardCharsets.UTF_8);
                BufferedReader br = new BufferedReader(isr);

                //循环读取来自服务器发送过来的消息并输出到客户端控制台上
                String message;
                while ((message = br.readLine())!=null){
                    System.out.println(message);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
